package cn.edu.scau.cmi.zhengweixin.singleton;

import java.util.ArrayList;

public class MarshalMultipleton {
	
	// 多例数量
	private static final int N = 5;
	// 存放N个实例对象的容器
	private static volatile ArrayList<Marshal> list = new ArrayList<>(N);

	public static ArrayList<Marshal> getList() {
		return list;
	}

	/**
	 * 随机获得实例对象
	 */
	public static Marshal getRandomInstance(String name) {
		if(list.size() < N) {
			synchronized(MarshalMultipleton.class) {
				if(list.size() < N) {
					list.add(new Marshal(name));
				}
			}
		}
		int x = (int)(Math.random()*list.size());
		return list.get(x);
	}

	static class Marshal{
		// 元帅的名字
		String name;

		private Marshal(String name) {
			super();
			this.name = name;
		}

		@Override
		public String toString() {
			return "[name=" + name + ", hashcode=" + this.hashCode() + "]";
		}
		
	}
	
}
